import { AxiosResponse } from 'axios';
import client from './config';

export function helloWorld(): Promise<string> {
  return client.get('/');
}

export function createTask(
  taskName: string
): Promise<NormalResponse<CreateTaskResponse>> {
  return client.post('/create_task', { taskName });
}

export function register(
  userName: string,
  passWord: string
): Promise<NormalResponse<null>> {
  return client.post('/register', { username: userName, password: passWord });
}

export function login(
  userName: string,
  passWord: string
): Promise<NormalResponse<null>> {
  return client.post('/login', { username: userName, password: passWord });
}

export function listTask(): Promise<NormalResponse<Task[]>> {
  return client.get('/get_all_task');
}

export function listTaskVersion(
  taskUuid: string
): Promise<NormalResponse<TaskVersion[]>> {
  return client.get('/v2/get_all_version?taskUuid=' + taskUuid);
}

export function createTaskVersion(
  taskName: string,
  versionName: string
): Promise<NormalResponse<{ version: TaskVersion }>> {
  return client.post('/create_task_version', { taskName, versionName });
}

export function getSourceFile(
  taskName: string,
  versionName: string
): Promise<NormalResponse<{ srcFile: SourceFile }>> {
  return client.get(
    `/get_src_file?taskName=${taskName}&versionName=${versionName}`
  );
}

export function addAllParameters(
  data: ConfigDetail
): Promise<NormalResponse<void>> {
  return client.post('/v2/add_all_parameters', data);
}

export function getAllVersion(
  taskUuid: string
): Promise<NormalResponse<TaskVersion[]>> {
  return client.get(`/v2/get_all_version?taskUuid=${taskUuid}`);
}

export function findAllParameters(
  versionUuid: string
): Promise<NormalResponse<ConfigDetail>> {
  return client.get(`/v2/find_all_parameters?versionUuid=${versionUuid}`);
}

export default {
  helloWorld,
  createTask,
  register,
  login,
  listTask,
  listTaskVersion,
  addAllParameters,
  findAllParameters,
};
